package org.jeecg.glr.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.glr.service.IGlrSensorDataService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 传感器数据统一处理
 * 昨天数据计算最大最小和平均值,保留最小ID记录，把计算数据更新到此条记录中
 * 定时任务
 */
@Slf4j
public class SensorJob implements Job {

    @Value("${spring.quartz.job.sensor_is_runing}")
    private String sensor_is_runing;

    @Autowired
    private IGlrSensorDataService glrSensorDataService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        long start = System.currentTimeMillis();
        String stJobName = "传感器数据处理";
        log.info(String.format("1处理" + stJobName + "定时任务开始!===时间:" + DateUtils.getTimestamp()));
        try {
            if (!"Y".equals(sensor_is_runing.toUpperCase())) {
                log.info("2===================处理" + stJobName + "定时任务取消===================");
                return;
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d2 = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(d2);
            calendar.add(calendar.DATE, -1);
            String stDate = sdf.format(calendar.getTime());
            glrSensorDataService.doComputeYesterdayData(stDate);

            log.info("3===================处理" + stJobName + "定时任务结束===================");

        } catch (Exception e) {
            e.printStackTrace();
            log.info("4===================ERROR处理" + stJobName + "定时务错误===================");
        }
        log.info(String.format("5处理" + stJobName + "定时任务结束!===时间:" + DateUtils.getTimestamp()));
        long end = System.currentTimeMillis();
        long times = end - start;
        log.info("6处理" + stJobName + "总耗时" + times + "毫秒");
    }


}
